package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.CompanySaleUserAddDTO;
import org.jsola.hr.dto.CompanySaleUserUpdateDTO;
import org.jsola.hr.entity.CompanySaleUserDO;
import org.jsola.hr.query.CompanySaleUserQuery;
import org.jsola.hr.service.ICompanySaleUserService;
import org.jsola.hr.vo.CompanySaleUserListVO;
import org.jsola.hr.vo.CompanySaleUserVO;
import org.jsola.hr.vo.CompanyVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 用工单位销售
 *
 * @author zhr
 */
@RestController("hrCompanySaleUserController")
@RequestMapping(HrConstants.API_PATH + "/v1/companySaleUsers")
public class CompanySaleUserController implements ICompanySaleUserControllerAPI {

    @Autowired
    private ICompanySaleUserService companySaleUserService;

    @Override
    @PostMapping
    public Result<CompanySaleUserVO> save(@Valid @RequestBody CompanySaleUserAddDTO companySaleUserAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增用工单位销售权限校验

        return Result.success(
                companySaleUserService.save(companySaleUserAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody CompanySaleUserUpdateDTO companySaleUserUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改用工单位销售权限校验

        int count = companySaleUserService.update(companySaleUserUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{companySaleUserId}")
    public Result<Integer> delete(@PathVariable String companySaleUserId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除用工单位销售权限校验

        int count = companySaleUserService.deleteByIds(tokenUser, companySaleUserId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] companySaleUserIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除用工单位销售权限校验

        int count = companySaleUserService.deleteByIds(tokenUser, companySaleUserIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{companySaleUserId}")
    public Result<CompanySaleUserVO> findById(@PathVariable String companySaleUserId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看用工单位销售权限校验

        return Result.success(
                companySaleUserService.selectById(companySaleUserId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<CompanySaleUserListVO>> listAll(@Valid CompanySaleUserQuery companySaleUserQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看用工单位销售权限校验

        return Result.success(
                companySaleUserService.select(companySaleUserQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<CompanySaleUserListVO>> page(@Valid CompanySaleUserQuery companySaleUserQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                companySaleUserService.selectPage(companySaleUserQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping("/selectSaleUserByCompanyId/{companyId}")
    public Result<List<CompanySaleUserDO>> selectSaleUserByCompanyId(@PathVariable String companyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                companySaleUserService.selectSaleUserByCompanyId(companyId, tokenUser.getSiteId())
        );
    }

    @GetMapping("/selectCompanyBySaleUserId/{userId}")
    public Result<List<CompanyVO>> selectCompanyBySaleUserId(@PathVariable String userId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                companySaleUserService.selectCompanyBySaleUserId(userId, tokenUser)
        );
    }
}

